// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.ai;

import java.util.ArrayList;
import zombie.iso.IsoGridSquare;
import zombie.iso.IsoObject;
import zombie.iso.objects.IsoDoor;
import zombie.iso.objects.IsoThumpable;
import zombie.iso.objects.IsoWindow;
import zombie.iso.objects.IsoWindowFrame;

public final class MapKnowledge {
    private final ArrayList<KnownBlockedEdges> knownBlockedEdges = new ArrayList();

    public ArrayList<KnownBlockedEdges> getKnownBlockedEdges() {
        return this.knownBlockedEdges;
    }

    public KnownBlockedEdges getKnownBlockedEdges(int x, int y, int z) {
        for (int _int = 0; _int < this.knownBlockedEdges.size(); _int++) {
            KnownBlockedEdges knownBlockedEdgesx = (KnownBlockedEdges)this.knownBlockedEdges.get(_int);
            if (knownBlockedEdgesx.x == x && knownBlockedEdgesx.y == y && knownBlockedEdgesx.z == z) {
                return knownBlockedEdgesx;
            }
        }

        return null;
    }

    private KnownBlockedEdges createKnownBlockedEdges(int int0, int int1, int int2) {
        assert this.getKnownBlockedEdges(int0, int1, int2) == null;

        KnownBlockedEdges knownBlockedEdgesx = KnownBlockedEdges.alloc();
        knownBlockedEdgesx.init(int0, int1, int2);
        this.knownBlockedEdges.add(knownBlockedEdgesx);
        return knownBlockedEdgesx;
    }

    public KnownBlockedEdges getOrCreateKnownBlockedEdges(int x, int y, int z) {
        KnownBlockedEdges knownBlockedEdgesx = this.getKnownBlockedEdges(x, y, z);
        if (knownBlockedEdgesx == null) {
            knownBlockedEdgesx = this.createKnownBlockedEdges(x, y, z);
        }

        return knownBlockedEdgesx;
    }

    private void releaseIfEmpty(KnownBlockedEdges knownBlockedEdgesx) {
        if (!knownBlockedEdgesx.n && !knownBlockedEdgesx.w) {
            this.knownBlockedEdges.remove(knownBlockedEdgesx);
            knownBlockedEdgesx.release();
        }
    }

    public void setKnownBlockedEdgeW(int x, int y, int z, boolean blocked) {
        KnownBlockedEdges knownBlockedEdgesx = this.getOrCreateKnownBlockedEdges(x, y, z);
        knownBlockedEdgesx.w = blocked;
        this.releaseIfEmpty(knownBlockedEdgesx);
    }

    public void setKnownBlockedEdgeN(int x, int y, int z, boolean blocked) {
        KnownBlockedEdges knownBlockedEdgesx = this.getOrCreateKnownBlockedEdges(x, y, z);
        knownBlockedEdgesx.n = blocked;
        this.releaseIfEmpty(knownBlockedEdgesx);
    }

    public void setKnownBlockedDoor(IsoDoor object, boolean blocked) {
        IsoGridSquare isoGridSquare = object.getSquare();
        if (object.getNorth()) {
            this.setKnownBlockedEdgeN(isoGridSquare.x, isoGridSquare.y, isoGridSquare.z, blocked);
        } else {
            this.setKnownBlockedEdgeW(isoGridSquare.x, isoGridSquare.y, isoGridSquare.z, blocked);
        }
    }

    public void setKnownBlockedDoor(IsoThumpable object, boolean blocked) {
        if (object.isDoor()) {
            IsoGridSquare isoGridSquare = object.getSquare();
            if (object.getNorth()) {
                this.setKnownBlockedEdgeN(isoGridSquare.x, isoGridSquare.y, isoGridSquare.z, blocked);
            } else {
                this.setKnownBlockedEdgeW(isoGridSquare.x, isoGridSquare.y, isoGridSquare.z, blocked);
            }
        }
    }

    public void setKnownBlockedWindow(IsoWindow object, boolean blocked) {
        IsoGridSquare isoGridSquare = object.getSquare();
        if (object.getNorth()) {
            this.setKnownBlockedEdgeN(isoGridSquare.x, isoGridSquare.y, isoGridSquare.z, blocked);
        } else {
            this.setKnownBlockedEdgeW(isoGridSquare.x, isoGridSquare.y, isoGridSquare.z, blocked);
        }
    }

    public void setKnownBlockedWindowFrame(IsoObject object, boolean blocked) {
        IsoGridSquare isoGridSquare = object.getSquare();
        if (IsoWindowFrame.isWindowFrame(object, true)) {
            this.setKnownBlockedEdgeN(isoGridSquare.x, isoGridSquare.y, isoGridSquare.z, blocked);
        } else if (IsoWindowFrame.isWindowFrame(object, false)) {
            this.setKnownBlockedEdgeW(isoGridSquare.x, isoGridSquare.y, isoGridSquare.z, blocked);
        }
    }

    public void forget() {
        KnownBlockedEdges.releaseAll(this.knownBlockedEdges);
        this.knownBlockedEdges.clear();
    }
}
